{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from time import time\n",
    "from sklearn import svm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading training data\n",
      "Loading testing data\n",
      "Loading validating data\n"
     ]
    }
   ],
   "source": [
    "print(\"Loading training data\")\n",
    "pd_train = pd.read_csv(\"../data/training\", header=None)\n",
    "\n",
    "print(\"Loading testing data\")\n",
    "pd_test = pd.read_csv(\"../data/testing\", header=None)\n",
    "\n",
    "print(\"Loading validating data\")\n",
    "pd_validate = pd.read_csv(\"../data/validation\", header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_X = pd_train.drop([0], axis=1).values / 255.0\n",
    "train_y = pd_train[0].values\n",
    "\n",
    "test_X = pd_test.drop([0], axis=1).values / 255.0\n",
    "test_y = pd_test[0].values\n",
    "\n",
    "validate_X = pd_validate.drop([0], axis=1).values / 255.0\n",
    "validate_y = pd_validate[0].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training time: 772.569\n"
     ]
    }
   ],
   "source": [
    "t0 = time()\n",
    "\n",
    "svm_rbf = svm.SVC(kernel='rbf')\n",
    "svm_rbf.fit(train_X, train_y)\n",
    "\n",
    "print(\"training time: %.3f\" % round(time() - t0, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pred_rbf = svm_rbf.predict(validate_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.945100\n",
      "\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       0.98      0.98      0.98       991\n",
      "          1       0.95      0.99      0.97      1064\n",
      "          2       0.94      0.94      0.94       990\n",
      "          3       0.93      0.92      0.93      1030\n",
      "          4       0.94      0.96      0.95       983\n",
      "          5       0.93      0.91      0.92       915\n",
      "          6       0.95      0.98      0.97       967\n",
      "          7       0.96      0.95      0.96      1090\n",
      "          8       0.94      0.90      0.92      1009\n",
      "          9       0.94      0.92      0.93       961\n",
      "\n",
      "avg / total       0.94      0.95      0.94     10000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import classification_report\n",
    "print(\"Accuracy: %f\\n\" % accuracy_score(validate_y, pred_rbf))\n",
    "print(classification_report(validate_y, pred_rbf))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training time: 514.096\n"
     ]
    }
   ],
   "source": [
    "t0 = time()\n",
    "\n",
    "svm_lin = svm.SVC(kernel='linear')\n",
    "svm_lin.fit(train_X, train_y)\n",
    "\n",
    "print(\"training time: %.3f\" % round(time() - t0, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pred_rbf = svm_rbf.predict(validate_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.945100\n",
      "\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       0.98      0.98      0.98       991\n",
      "          1       0.95      0.99      0.97      1064\n",
      "          2       0.94      0.94      0.94       990\n",
      "          3       0.93      0.92      0.93      1030\n",
      "          4       0.94      0.96      0.95       983\n",
      "          5       0.93      0.91      0.92       915\n",
      "          6       0.95      0.98      0.97       967\n",
      "          7       0.96      0.95      0.96      1090\n",
      "          8       0.94      0.90      0.92      1009\n",
      "          9       0.94      0.92      0.93       961\n",
      "\n",
      "avg / total       0.94      0.95      0.94     10000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "print(\"Accuracy: %f\\n\" % accuracy_score(validate_y, pred_rbf))\n",
    "print(classification_report(validate_y, pred_rbf))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
